//
//  InspectionLineView.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/27.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "InspectionLineView.h"
#import "InspectionLineCell.h"
#define MIN_TABLE_VIEW_HEIGHT 100

@interface InspectionLineView() <UITableViewDelegate, UITableViewDataSource> {
    
}

@property (strong, nonatomic) UIView *titleView;
@property (strong, nonatomic) InspectionLineView *contentView;
@property (strong, nonatomic) UITableView *lineTableView;

@property (strong, nonatomic) UIView *bottomBtnView;
@property (strong, nonatomic) UIButton *confirmBtn;
@property (strong, nonatomic) UIButton *cancelBtn;

@property (nonatomic, assign) CGFloat viewHeight;
@property (nonatomic, assign) CGFloat titleHeight;
@property (nonatomic, assign) CGFloat bottomHeight;

@property (nonatomic, strong) InspectionLine* selectLine;
@end

@implementation InspectionLineView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _titleHeight = 50;
        _bottomHeight = 80;
        
        _viewHeight = frame.size.height;
        if ((_viewHeight - _titleHeight - _bottomHeight) < MIN_TABLE_VIEW_HEIGHT) {
            _viewHeight = MIN_TABLE_VIEW_HEIGHT + _titleHeight + _bottomHeight;
            self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
        }
        
        [self setupTitleView];
        [self setupBtnView];
        [self setupLineTableView];
    }
    return self;
}

-(void) setupTitleView {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _titleHeight)];
    UILabel* label = [[UILabel alloc] initWithFrame:view.frame];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"请选择线路";
    [view addSubview:label];
    
    UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_right_arrow"]];
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).offset(10);
        make.right.equalTo(view).offset(-20);
    }];
    
    UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor = HEXCOLOR(0XCCCCCC);
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view);
        make.top.equalTo(view.mas_bottom).offset(-0.5);
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    _titleView = view;
    [self addSubview:_titleView];
}

-(void) setupLineTableView {
    _lineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _titleHeight, SCREEN_WIDTH, 200)];
    [self addSubview:_lineTableView];
    _lineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_lineTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(_titleView.mas_bottom);
        make.height.mas_equalTo(_viewHeight - _bottomHeight - _titleHeight);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    _lineTableView.backgroundColor = UIColor.whiteColor;
    _lineTableView.delegate = self;
    _lineTableView.dataSource = self;
}

-(void) setupBtnView {
    CGFloat height = 30;
    CGFloat width = 80;
    CGFloat offsetX = 100;
    
    CGFloat offsetY = (_bottomHeight - height) / 2.0;
    CGFloat offsetTop = _viewHeight - _bottomHeight;
    
    _bottomBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, offsetTop, SCREEN_WIDTH, _bottomHeight)];
    //_bottomBtnView.backgroundColor = [UIColor blueColor];
    
    _confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(offsetX, offsetY, width, height)];
    [_confirmBtn setTitle:@"确  定" forState:UIControlStateNormal];
    _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _confirmBtn.titleLabel.textColor = [UIColor whiteColor];
    _confirmBtn.layer.cornerRadius = 15;
    _confirmBtn.layer.masksToBounds = YES;
    _confirmBtn.backgroundColor = HEXCOLOR(0x5A7EEE);
    
    _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - offsetX - width, offsetY, width, height)];
    [_cancelBtn setTitle:@"取  消" forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _cancelBtn.titleLabel.textColor = HEXCOLOR(0x5A7EEE);
    _cancelBtn.layer.cornerRadius = 15;
    _cancelBtn.layer.masksToBounds = YES;
    _cancelBtn.backgroundColor = HEXACOLOR(0x555A7EEE);
    
    [_bottomBtnView addSubview:_confirmBtn];
    [_bottomBtnView addSubview:_cancelBtn];
    
    [self addSubview:_bottomBtnView];
    
    CommonWeakSelf;
    [_confirmBtn addGestureRecognizer:GestureType_Tap block:^(__kindof UIView * _Nonnull view, __kindof UIGestureRecognizer * _Nonnull gesture) {
        
        CommonStrongSelf;
        if (strongSelf.selectBtn) {
            strongSelf.selectBtn(YES, strongSelf.selectLine);
        }
    }];
    
    [_cancelBtn addGestureRecognizer:GestureType_Tap block:^(__kindof UIView * _Nonnull view, __kindof UIGestureRecognizer * _Nonnull gesture) {
        
       CommonStrongSelf;
       if (strongSelf.selectBtn) {
           strongSelf.selectBtn(NO, nil);
       }
    }];
}

//dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InspectionLineCell *cell = (InspectionLineCell*) [tableView dequeueReusableCellWithIdentifier:@"InspectionLineCellIdentifier"];
    
    if (cell == nil) {
        [_lineTableView registerNib:[UINib nibWithNibName:@"InspectionLineCell" bundle:nil] forCellReuseIdentifier:@"InspectionLineCellIdentifier"];
        cell = [_lineTableView dequeueReusableCellWithIdentifier:@"InspectionLineCellIdentifier"];
    }
    
    InspectionLine* line = self.inspecionLines[indexPath.row];
    [cell setLineName:line.name];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.inspecionLines.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _selectLine = self.inspecionLines[indexPath.row];
    //[Toast toast:line.name];
}

@end
